package demo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TestBinaryTree {
    static class TreeNode {
        public char val; //数据域
        public TreeNode left; //左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }
    //public TreeNode root; //二叉树的根节点

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        //this.root = A;
        return A;
    }

    //前序遍历  根 - 左 - 右
    public void preOrder1(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //子问题解决  - 前序遍历
    public List<Character> preOrder(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if  (root == null) {
            return list;
        }
        list.add(root.val);
        List<Character> left = preOrder(root.left);
        list.addAll(left);
        List<Character> right = preOrder(root.right);
        list.addAll(right);
        return list;
    }
    //子问题解决  - 中序遍历
    public List<Character> inorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        List<Character> left = inorderTraversal(root.left);
        list.addAll(left);
        list.add(root.val);
        List<Character> right = inorderTraversal(root.right);
        list.addAll(right);
        return list;
    }
    //子问题解决思路 - 后序遍历
    public List<Character> postorderTraversal(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        List<Character> left = postorderTraversal(root.left);
        list.addAll(left);
        List<Character> right = postorderTraversal(root.right);
        list.addAll(right);
        list.add(root.val);
        return list;
    }


    // 获取树中节点的个数
    int size(TreeNode root){
        int count = 0;
        if (root == null) {
            return count;
        }
        int leftcount = size(root.left);
        count += leftcount;
        int rightcoutn = size(root.right);
        count += rightcoutn;
        return ++count;
    }
    // 获取叶子节点的个数
    int getLeafNodeCount(TreeNode root){
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }
    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k){
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int leftTree = getKLevelNodeCount(root,k - 1);
        int rightTree = getKLevelNodeCount(root,k - 1);
        return leftTree + rightTree;
    }
    // 获取二叉树的高度
    int getHeight(TreeNode root){
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }
    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if (root == null) {
            return null;
        }
        if (val == root.val) {
            return root;
        }
        TreeNode left = find(root,val);
        if (left != null) {
            return left;
        }
        TreeNode right = find(root,val);
        return right;
    }

    //相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        boolean left = isSameTree(p.left,q.left);
        if (!left) {
            return false;
        }
        boolean right = isSameTree(p.right,q.right);
        return right;

    }

    //另一颗树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null && subRoot == null) {
            return true;
        }
        if (root == null || subRoot == null) {
            return false;
        }
        boolean s = isSubtrees(root,subRoot);
        if (s) {
            return true;
        }

        boolean left = isSubtree(root.left,subRoot);
        if (left) {
            return true;
        }

        boolean right = isSubtree(root.right,subRoot);
        return right;
    }
    private boolean isSubtrees(TreeNode root, TreeNode subRoot) {
        if (root == null && subRoot == null) {
            return true;
        }
        if (root == null || subRoot == null) {
            return false;
        }
        if (root.val != subRoot.val) {
            return false;
        }
        boolean left = isSubtrees(root.left,subRoot.left);
        if (!left) {
            return false;
        }
        boolean right = isSubtrees(root.right,subRoot.right);
        return right;
    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        invertTrees(root);
        return root;
    }
    private void invertTrees(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
    }

    //平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int n = height(root);
        return n != -1;
    }
    public int height(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = height(root.left);
        if (left == -1) {
            return -1;
        }
        int right = height(root.right);
        if (right == -1 || Math.abs(left - right) >= 2) {
            return -1;
        }
        return left > right ? left + 1 : right + 1;

    }
}
